package com.travel.service;

import com.travel.config.RabbitConfig;
import com.travel.mapper.OrderMapper;
import com.travel.pojo.Order;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

@Service
public class OrderReceiver {

    private final RabbitTemplate rabbitTemplate;

    OrderMapper orderMapper;

    public OrderReceiver(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @RabbitListener(queues = RabbitConfig.ORDER_QUEUE_NAME)
    public void receiveOrder(Order order) {
        order.setStatus(1);
        orderMapper.updateById(order);
    }

    @RabbitListener(queues = RabbitConfig.DEAD_LETTER_QUEUE_NAME)
    public void receiveDeadLetterOrder(Order order) {
        order.setStatus(3);
        orderMapper.updateById(order);
    }

    @RabbitListener(queues = RabbitConfig.PAYMENT_SUCCESS_QUEUE_NAME)
    public void receivePaymentSuccessMessage(String orderId) {
        Order order = orderMapper.selectById(orderId);
        if (order != null) {
            order.setStatus(2);  // assuming status 2 means payment success
            orderMapper.updateById(order);
        }
    }
}
